<template>
  <van-popup
    :style="{ height: '40%' }"
    position="bottom"
    v-model="show"
  >
    <van-datetime-picker
      :max-date="maxDate"
      :min-date="minDate"
      title="选择年月日"
      @confirm="onConfirm"
      @cancel="onCancel"
      type="date"
      v-model="currentDate"
    />
  </van-popup>
</template>

<script>
import { updateUserProfile } from "../../../network/user"
import dayjs from 'dayjs'
export default {
  name: "EditorBirth",
  props:['info'],
  components: {},
  computed: {},
  watch:{
    info() {
      this.currentDate = new Date(this.info.birthday)
    },
  },
  data() {
    return {
      show:false,
      minDate: new Date(1970, 0, 1),
      maxDate: new Date(),
      currentDate: new Date(),
    };
  },
  created() {},
  mounted() {},
  methods: {
    isShow() {
      this.show = true
    },
    async onConfirm(value){
      const birthday = dayjs(value).format('YYYY-MM-DD')
      // 请求更新
      const result = await updateUserProfile({
        'birthday': birthday
      })
      if(result.message === "OK"){
        // 更新视图
        this.info.birthday = birthday
      }else{
        this.$toast.fail('更新失败')
      }      
      // 关闭弹层
      this.show = false
    },
    onCancel() {
      this.currentDate = new Date(this.info.birthday)
      this.show = false
    }
  }
};
</script>

<style scoped lang >
</style>